package Servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.bcel.classfile.ParameterAnnotationEntry;

import DAO.DBCon;

/**
 * Servlet implementation class EDIChange
 */
@WebServlet("/EDIChange")
public class EDIChange extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EDIChange() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String dept=request.getParameter("Dname2");
		String sql = "select workID from ebit91 where Dname=\""+dept+"\"";
		String sql1 ;
		String sql2 ;
		String sql3 ;
		String sql4 ,sql5;
		
		DBCon dbCon = new DBCon();
		Connection connection = dbCon.getConnection();
		try{
			PreparedStatement stmt = connection.prepareStatement(sql);
			ResultSet rs1 = stmt.executeQuery();
			while(rs1.next()){
				String workID = rs1.getString("workID");
			    sql1 = "delete from eeit91 where workID=\""+workID+"\"";
				sql2 = "delete from emst91 where workID=\""+workID+"\"";
				PreparedStatement stmt1 = connection.prepareStatement(sql1);
				PreparedStatement stmt2 = connection.prepareStatement(sql2);
				stmt1.executeUpdate();
				stmt2.executeUpdate();
				
				stmt1.close();
				stmt2.close();
			}
			
			sql3 = "delete from ebit91 where Dname=\""+dept+"\"";
			sql4 = "delete from ejpt91 where Dname=\""+dept+"\"";
			sql5 = "delete from edit91 where Dname=\""+dept+"\"";
			
			PreparedStatement stmt3 = connection.prepareStatement(sql3);
			PreparedStatement stmt4 = connection.prepareStatement(sql4);
			PreparedStatement stmt5 = connection.prepareStatement(sql5);
			stmt3.executeUpdate();
			stmt4.executeUpdate();
			stmt5.executeUpdate();
			
			stmt3.close();
			stmt4.close();
			stmt5.close();
			rs1.close();
			connection.close();
			
			response.setHeader("refresh", "1;url=EDIChange.jsp");
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
